<template>
  <!-- 订单添加修改页面 -->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增订单' : '编辑订单'" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
<!--      <el-form-item prop="orderStatus" label="订单状态">-->
<!--        <ren-select v-model="dataForm.orderStatus" disabled dict-type="orderStatus" placeholder="请选择订单状态"></ren-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveName" label="收货人">-->
<!--        <el-input v-model="dataForm.receiveName" placeholder="请输入收货人的姓名" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receivePhone" label="手机号码">-->
<!--        <el-input v-model="dataForm.receivePhone" placeholder="请输入收货人的手机号码" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveProvince" label="所在省份">-->
<!--        <el-input v-model="dataForm.receiveProvince" placeholder="请输入收货地址所在的省份" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveCity" label="所在城市">-->
<!--        <el-input v-model="dataForm.receiveCity" placeholder="请输入收货地址所在的城市" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveCounty" label="所在区县">-->
<!--        <el-input v-model="dataForm.receiveCounty" placeholder="请输入收货地址所在的区县" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="receiveAddress" label="详细地址">-->
<!--        <el-input v-model="dataForm.receiveAddress" placeholder="请输入详细的街道地址，如门牌号、楼层等" clearable></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="remark" label="订单备注">-->
<!--        <el-input v-model="dataForm.remark" placeholder="请输入订单备注" clearable></el-input>-->
<!--      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      url: '',
      dialogImageUrl: '',
      dialogVisible: false,
      imagelist:{},
      dataForm: {
        id: '',                         //订单id
        productId: '',                  //兑换的商品ID
        userId:'',                      //用户id
        specsId: '',                    //商品规格id
        imgUrl: '',                     //图片
        productName: '',                //被购买商品的名称
        specsDesc: '',                  //规格描述
        createDateStart: '',            //开始创建时间
        createDateEnd: '',              //结束创建时间
        deliveryDateStart:'',           //开始发货时间
        deliveryDateEnd: '',            //结束发货时间
        orderStatus: '',                //订单的当前状态，(1) 待发货；(2) 已发货；
        receiveName: '',                //记录收货人的姓名。
        receivePhone: '',               //记录收货人的手机号码
        receiveProvince: '',            //记录收货地址所在的省、州或自治区
        receiveCity: '',                //记录收货地址所在的城市
        receiveCounty: '',              //记录收货地址所在的区县
        receiveAddress:'',              //记录详细的街道地址，如门牌号、楼层等
        totalPoints: '',                //支付积分
        expressCompany: '',             //物流公司编码，快递公司ID，参见「查询物流公司编码列表」，物流快递发货时必填， 示例值: DHL 字符字节限制: [1, 128]
        trackingNo: '',                 //物流单号，物流快递发货时必填，示例值: 323244567777 字符字节限制: [1, 128]
        logisticsType: '',              //物流模式
      }
    }
  },
  computed: {
    dataRule() {
      return {
        orderStatus: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receivePhone: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveProvince: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveCity: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveCounty: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        receiveAddress: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        remark: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/userOrder/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/userOrder', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
<style>
.el-upload el-upload-picture-card {
  display: none;
}
</style>
